package jsonParser;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ruiyi.baseparser.Parser;
import com.ruiyi.baseparser.RuiyiType;
import com.ruiyi.error.RuiyiException;


public class _TestParser {
	private static final String TAG = "CRead";
	public static final String URL = "http://api.locoso.com/v1/";

	private static final int TIME_OUT = 5 * 1000;
	private static final int CLIENT_BUFFER_SIZE = 8192;
	
	public static void main(String[] argz){
		test();
	}
	private static void test() {
		long cityid = 10000;
		String url = "city.action?act=getmodel&id=" + cityid;
		try {
			parse(new CityParser(), getContent(url));			
		} catch (RuiyiException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// *********************************************************************************************
	public static RuiyiType parse(Parser<? extends RuiyiType> parser, String content) throws RuiyiException{        
        try {
        	log("http response: " + content);
        	if(content.charAt(0) == '['){
        		log("parse array");
            	JSONArray array = new JSONArray(content);            	
            	return parser.parse(array);
        	}
        	else{
        		log("parse object");
            	JSONObject obj = new JSONObject(content);            	
            	return parser.parse(obj);       		
        	}
        } catch (JSONException ex) {
            throw new RuiyiException("Error parsing JSON response: " + ex.getMessage());
        }        
	}
	// *********************************************************************************************
	private static String getContent(String url) throws Exception {
		url = URL + url;
		StringBuilder sb = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();

		log("url:" + url);

		HttpConnectionParams.setConnectionTimeout(httpParams, TIME_OUT); // 设置网络超时参数
		HttpConnectionParams.setSoTimeout(httpParams, TIME_OUT);
		HttpResponse response = client.execute(new HttpGet(url));
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					entity.getContent(), "UTF-8"), CLIENT_BUFFER_SIZE);
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			reader.close();
		}

		String result = sb.toString();
		log("getContent():" + result);

		return result;
	}
	// *******************************************************************************************
	public static void log(Object obj){
		System.out.println(TAG + ":\t" + obj);
	}
	public static void log(){
		System.out.println();		
	}
	// *********************************************************************************************
}
